package cn.com.acca.ma.dao.impl;

import cn.com.acca.ma.dao.MacroChinaGDPDao;
import cn.com.acca.ma.hibernate.util.HibernateUtil;
import cn.com.acca.ma.model.MacroChinaGDP;
import org.hibernate.Query;
import org.hibernate.SQLQuery;

import java.util.List;

public class MacroChinaGDPDaoImpl extends BaseDaoImpl<MacroChinaGDP> implements MacroChinaGDPDao {

    public MacroChinaGDPDaoImpl() {
        super();
    }

    /**
     * 查询年份列表，并按升序排列
     * @return
     */
    @Override
    public List<String> orderByDistinctYearList() {
        logger.info("开始查询年份列表，并按升序排列");

        String hql = "select distinct substr(t.quarter, 0, 4) from MacroChinaGDP t order by substr(t.quarter, 0, 4) asc";
        session = HibernateUtil.currentSession();
        session.beginTransaction();
        Query query = session.createQuery(hql);
        List<String> list = query.list();
        session.getTransaction().commit();
        session.close();

        logger.info("查询年份列表，并按升序排列完成");

        return list;
    }

    /**
     * 根据quarter查询记录
     * @param quarter
     * @return
     */
    @Override
    public MacroChinaGDP findByQuarter(String quarter) {
        logger.info("开始根据quarter查询记录");

        String hql = "select t from MacroChinaGDP t where t.quarter=?";
        session = HibernateUtil.currentSession();
        session.beginTransaction();
        Query query = session.createQuery(hql);
        query.setParameter(0, quarter);
        MacroChinaGDP macroChinaGDP = (MacroChinaGDP) query.uniqueResult();
        session.getTransaction().commit();
        session.close();

        logger.info("根据quarter查询记录完成");

        return macroChinaGDP;
    }
}
